package com.bawei.zk_01

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.zk_01.ViewModel.LoginViewModel
import com.bawei.zk_01.databinding.ActivityResetBinding
import com.blankj.utilcode.util.ToastUtils

@Route(path = "/app/ResetActivity")

class ResetActivity : BaseAcitivity<ActivityResetBinding,LoginViewModel>() {
    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_reset
    }

    override fun initView() {

        //获取验证码
        binding.bt1.setOnClickListener {
            viewModel.getCode(binding.et1.text.toString())
        }

        //修改密码
        binding.bt2.setOnClickListener {
            viewModel.xiugai(binding.et1.text.toString(),binding.et2.text.toString(),binding.et3.text.toString())
        }

    }

    @SuppressLint("NewApi", "NotificationPermission")
    override fun initData() {

        viewModel.codesuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("获取成功")
                //验证码通知
                val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(NotificationChannel("aa","aa",NotificationManager.IMPORTANCE_DEFAULT))
                val builder = NotificationCompat.Builder(this, "aa")
                    .setContentTitle("验证码")
                    .setContentText("修改密码的验证码${it.data.content.substring(8,14)}")
                    .setSmallIcon(R.drawable.img)
                    .setAutoCancel(true)
                notificationManager.notify(1,builder.build())
            }
        }

        viewModel.codefail.observe(this){
            ToastUtils.showLong(it)
        }

        viewModel.xgsuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("修改成功")
                ARouter.getInstance().build("/app/LoginActivity").navigation()
                finish()
            }
        }

        viewModel.xgfail.observe(this){
            ToastUtils.showLong(it)
        }

    }

}